﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SerializedData;
using KnowledgeBaseIntermediary;

namespace KnowledgeBaseManager
{
    public partial class CState : Form
    {
        private DataCollection mDataCollection = DataCollection.Instance;
        private SState mState;
        
        public CState(SState state)
        {
            InitializeComponent();
            mState = state;
            textBox1.Text = mState.getName();
            textBox2.Text = mState.getDescription();
            textBox3.Text = mState.getTts();
            comboBox1.Items.Add(StateDTO.PRIORITY.ALERT.ToString());
            comboBox1.Items.Add(StateDTO.PRIORITY.HIGHEST.ToString());
            comboBox1.Items.Add(StateDTO.PRIORITY.HIGH.ToString());
            comboBox1.Items.Add(StateDTO.PRIORITY.MEDIUM.ToString());
            comboBox1.Items.Add(StateDTO.PRIORITY.LOW.ToString());
            comboBox1.Items.Add(StateDTO.PRIORITY.LOWEST.ToString());
            comboBox1.Text = mState.getPriority().ToString();
            checkBox1.Checked = mState.isChecked();
            refreshAll();
        }

        public void refreshAll()
        {
            refreshAutoCommands();
            refreshManualCommands();
            refreshScenes();
        }

        private void refreshAutoCommands()
        {
            listViewAC.Clear();
            listViewAC.BeginUpdate();
            listViewAC.Columns.Add("Name", 250);
            listViewAC.Columns.Add("Description", 250);

            foreach (SCommand command in mState.getAutoCommands())
            {
                ListViewItem oneRow = listViewAC.Items.Add(command.getName());
                oneRow.SubItems.Add(command.getDescription());
            }
            listViewAC.EndUpdate();
        }

        private void refreshScenes()
        {
            listViewS.Clear();
            listViewS.BeginUpdate();
            listViewS.Columns.Add("Name", 250);
            listViewS.Columns.Add("Description", 250);

            foreach (SScene scene in mState.getScenes())
            {
                ListViewItem oneRow = listViewS.Items.Add(scene.getName());
                oneRow.SubItems.Add(scene.getDescription());
            }
            listViewS.EndUpdate();
        }

        private void refreshManualCommands()
        {
            listViewMC.Clear();
            listViewMC.BeginUpdate();
            listViewMC.Columns.Add("Name", 250);
            listViewMC.Columns.Add("Description", 250);

            foreach (SCommand command in mState.getManualCommands())
            {
                ListViewItem oneRow = listViewMC.Items.Add(command.getName());
                oneRow.SubItems.Add(command.getDescription());
            }
            listViewMC.EndUpdate();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            mState.setName(textBox1.Text);
            mState.setDescription(textBox2.Text);
            mState.setTts(textBox3.Text);
            mState.setChecked(checkBox1.Checked);
            if (comboBox1.Text.Equals(StateDTO.PRIORITY.LOWEST.ToString()))
            {
                mState.setPriority(StateDTO.PRIORITY.LOWEST);
            }
            else if (comboBox1.Text.Equals(StateDTO.PRIORITY.LOW.ToString()))
            {
                mState.setPriority(StateDTO.PRIORITY.LOW);
            }
            else if (comboBox1.Text.Equals(StateDTO.PRIORITY.HIGH.ToString()))
            {
                mState.setPriority(StateDTO.PRIORITY.HIGH);
            }
            else if (comboBox1.Text.Equals(StateDTO.PRIORITY.HIGHEST.ToString()))
            {
                mState.setPriority(StateDTO.PRIORITY.HIGHEST);
            }
            else if (comboBox1.Text.Equals(StateDTO.PRIORITY.ALERT.ToString()))
            {
                mState.setPriority(StateDTO.PRIORITY.ALERT);
            }
            else 
            {
                mState.setPriority(StateDTO.PRIORITY.MEDIUM);
            } 
            
            mDataCollection.modifyOrAddState(mState);
            ((MainWindow)this.Owner).refreshStateList();
            this.Close();
        }


        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddAutoCommandToState ac = new AddAutoCommandToState(mState);
            ac.Owner = this;
            ac.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddSceneToState ac = new AddSceneToState(mState);
            ac.Owner = this;
            ac.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddManualCommandToState ac = new AddManualCommandToState(mState);
            ac.Owner = this;
            ac.ShowDialog();
        }
    }
}
